package main

import (
	"chapter-01-hello/service"
	"context"
	"fmt"
	"google.golang.org/grpc"
	"net"
)

type server struct {
	service.UnimplementedActionServer //未实现的方法
}

func (*server) SayHello(ctx context.Context, req *service.HelloRequest) (*service.HelloResponse, error) {
	fmt.Println("HelloRequest: ", req.Id)
	return &service.HelloResponse{
		Name: "yangchao",
		Age:  "18",
	}, nil
}

func main() {
	//获取当前目录
	//dir, err := os.Getwd()
	//if err != nil {
	//	grpclog.Fatalf("Failed to get current directory: %v", err)
	//}
	////配置 TLS认证相关文件
	//creds, err := credentials.NewServerTLSFromFile(path.Join(dir, "/key/server.pem"), path.Join(dir, "/key/server.key"))
	//if err != nil {
	//	grpclog.Fatalf("Failed to generate credentials %v", err)
	//}

	listen, err := net.Listen("tcp", ":9090")
	if err != nil {
		panic(err)
	}
	//grpcServer := grpc.NewServer(grpc.Creds(creds))
	grpcServer := grpc.NewServer() //非认证TLS
	// 注册服务
	service.RegisterActionServer(grpcServer, &server{})
	fmt.Println("grpc server start")
	err = grpcServer.Serve(listen)
	if err != nil {
		panic(err)
	}
}
